const app = getApp()
import {request} from "../../utils/request";
Page({
    data: {
        custom:wx.getMenuButtonBoundingClientRect(),
        value1: [],
        options2: [],
        value2: [],
        visible2:false,
        title2:'',
        province:'',
        city:'',
        county:'',
        name:'',
        phone:'',
        address:'',
        main:0,
        id:''
    },
    onLoad(options) {
        this.getRegion()
        if('id' in options){
            this.getAddress(options.id)
        }
    },
    onOpen1() {
        this.setData({ visible2: true })
    },
    onClose1() {
        this.setData({ visible2: false })
    },
    onChange1(e) {
        if(e.detail.options[0]){
            this.setData({province:e.detail.options[0].label,})
        }
        if(e.detail.options[1]){
            this.setData({city:e.detail.options[1].label,})
        }
        if(e.detail.options[2]){
            this.setData({county:e.detail.options[2].label})
        }
    },
    getRegion(){
        request({url:"/miniprogram/system/asyncRegion"}).then(res=>{this.setData({options2:res.data})})
    },
    changeValue(e) {
        var field = e.currentTarget.dataset.field,
            value = e.detail.value
        this.setData({[field]: value})
    },
    changeMain(){
        this.setData({main:this.data.main ? 0 : 1})
    },
    saveAddress(){
        var user = wx.getStorageSync('userInfo')
        if(!user){
            //没获取到用户信息，要重新登录
            return false
        }
        request({
            url:"/miniprogram/user/editAddress",
            data:{
                id:this.data.id,
                user_id:user.id,
                name:this.data.name,
                phone:this.data.phone,
                province:this.data.province,
                city:this.data.city,
                county:this.data.county,
                address:this.data.address,
                main:this.data.main,
            },
            type:'post'
        }).then(res=>{
            wx.showToast({title:res.msg,success:res=>{
                setTimeout(()=>{wx.navigateBack()},1500)
                }})
        })
    },
    chooseAddress(){
        var user = wx.getStorageSync('userInfo')
        if(!user){
            //没获取到用户信息，要重新登录
            return false
        }
        wx.chooseAddress({success:(res)=>{
            if(res.errMsg == "chooseAddress:ok"){
                request({
                    url:"/miniprogram/user/editAddress",
                    data:{
                        user_id:user.id,
                        name:res.userName,
                        phone:res.telNumber,
                        province:res.provinceName,
                        city:res.cityName,
                        county:res.countyName,
                        address:res.detailInfo,
                        main:this.data.main,
                    },
                    type:'post'
                }).then(res=>{
                    wx.showToast({title:res.msg,success:res=>{
                            setTimeout(()=>{wx.navigateBack()},1500)
                        }})
                })
            }
            }})
    },
    getAddress(id){
        request({url:"/miniprogram/user/editAddress?id="+id}).then(res=>{
            this.setData({
                name:res.data.name,
                phone:res.data.phone,
                province:res.data.province,
                city:res.data.city,
                county:res.data.county,
                address:res.data.address,
                main:res.data.main,
                id:res.data.id
            })
        })
    }
})